package com.jrx.anytxn.accounting.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.accounting.dto.VoucherExcpReportRes;
import com.jrx.anytxn.accounting.entity.GaVoucherExcpReport;
import com.jrx.anytxn.accounting.mapper.ext.ExtVoucherExcpReportMapper;
import com.jrx.anytxn.accounting.service.IVoucherExcpReportService;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.param.constant.Constant;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 传票例外报表 的接口实现类
 *
 * @author Qingyao Xing
 * @date 2019/9/6 13:24
 */
@Service
public class VoucherExcpReportServiceImpl implements IVoucherExcpReportService {
    private static final Logger logger = LoggerFactory.getLogger(VoucherExcpReportServiceImpl.class);
    @Resource
    private ExtVoucherExcpReportMapper extVoucherExcpReportMapper;

    @Override
    public VoucherExcpReportRes getModalByIdAndTenIdAndCusId(Long id, String tenantId, String customerId) throws TxnException {
        if (id == null && StringUtils.isBlank(tenantId)) {
            logger.error("传票例外报表主键ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        GaVoucherExcpReport voucherExcpReport = extVoucherExcpReportMapper.selectModalByIdAndTenIdAndCusId(id, tenantId, customerId);
        if (null == voucherExcpReport) {
            logger.error("根据主键[{}]查询的传票例外报表集合为空", id);
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        return BeanMapping.copy(voucherExcpReport, VoucherExcpReportRes.class);
    }

    @Override
    public TxnPage<VoucherExcpReportRes> getPage(Integer pageNum, Integer pageSize, String tenantId, String startDate, String endDate, String loanId, String customerId,String channel) throws TxnException {
        Page<VoucherExcpReportRes> page = PageHelper.startPage(pageNum, pageSize);
        List<GaVoucherExcpReport> voucherExcpReports = extVoucherExcpReportMapper.selectAllByPageAndTenId(tenantId,startDate,endDate,loanId,customerId,channel);
        if (voucherExcpReports.isEmpty()) {
            logger.error("查询的传票例外报表列表为空");
            throw new TxnException(TxnRespCode.SUCCESS.getCode(), "传票例外报表没有数据");
        }
        List<VoucherExcpReportRes> res = BeanMapping.copyList(voucherExcpReports, VoucherExcpReportRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public void deleteByDate(Date businessDate) {
        extVoucherExcpReportMapper.deleteByDate(businessDate);
    }
}
